@echo off
set IMAGE_NAME=mssql2mysql
set CONTAINER_NAME=mssql2mysql
set TEMP_IMAGE_NAME=%IMAGE_NAME%-temp

echo --- Step 1: Building new temporary image: %TEMP_IMAGE_NAME% ---
docker build -t %TEMP_IMAGE_NAME% .
if %errorlevel% neq 0 (
    echo Docker build failed!
    pause
    exit /b 1
)

echo.
echo --- Step 2: Stopping and removing existing container: %CONTAINER_NAME% ---
docker stop %CONTAINER_NAME% >nul 2>&1
docker rm %CONTAINER_NAME% >nul 2>&1

echo.
echo --- Step 3: Removing old image: %IMAGE_NAME% ---
docker rmi %IMAGE_NAME% >nul 2>&1

echo.
echo --- Step 4: Tagging temporary image to final name: %IMAGE_NAME% ---
docker tag %TEMP_IMAGE_NAME% %IMAGE_NAME%
docker rmi %TEMP_IMAGE_NAME%

echo.
echo --- Step 5: Running new container: %CONTAINER_NAME% ---
docker run -d --name %CONTAINER_NAME% -p 7631:7629 -v "%cd%\data:/app/data" %IMAGE_NAME%

echo.
echo --- Redeployment complete! ---
echo Application is now running at:
echo   Main Page: http://localhost:7631
echo   Admin Panel: http://localhost:7631/admin
echo   Admin Login: admin / admin123
pause